import sparkMd5 from 'spark-md5';

export const isNode = !(window && window.document)

export const fetch = (...args) => {
	if(isNode){
		const nodeFetch = require('node-fetch').default
		return nodeFetch(...args);
	} else {
		return window.fetch(...args);
	}
}

export const md5 = sparkMd5.hash
export const decodeBase64 = str => {
	if( isNode ){
		return new Buffer(str, 'base64').toString()
	} else {
		return window.atob(str)
	}
}
export const encodeBase64 = str => {
	if( isNode ){
		return new Buffer(str).toString('base64')
	} else {
		return window.btoa(str)
	}
}